﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Entity;
using NLBS.Framework;
using NLBS.Resources;
using NLBS.Views;

namespace NLBS.Validate
{
    public delegate void CallValid();

    

   public abstract class ValidateTemplete<T>
    {
       protected Right power = BlogInfo.Instance().CurrentUser.Group.Power;
       protected Dictionary<string, string> lang = Language.Instance().getLanguage();

       private Action _act;
       public Action Act
       {
           get
           {
               return _act;
           }
           set
           {
               _act = value;
           }
       }
       private T _entity;
       public T Entity
       {
           get
           {
               return _entity;
           }
           set
           {
               _entity = value;
           }
       }

       private int _keyID;
       public int keyID
       {
           get
           {
               return _keyID;
           }
           set
           {
               _keyID = value;
           }
       }


       private string _error;
       public string Error
       {
           get { return _error; }
           set { _error = value; }
       }
      
       public virtual void Validate(T obj, CallValid valid, CallError error)
       {
           try
           {
               this.Entity = obj;
               checkPower();
               checkArgument();
               checkEntity();
               checkPowerAgain();
           }
           catch (System.Exception e)
           {
               error(e.Message);
           }
           valid();
       }

       public void ShowError(string error)
       {
           Error = error;
           throw new Exception(error);
       }

       public virtual bool Validate(T obj)
       {
           try
           {
               this.Entity = obj;
               checkPower();
               checkArgument();
               checkEntity();
               checkPowerAgain();
           }
           catch (System.Exception e)
           {
               Error = e.Message;
               return false;
           }
           return true;
       }

       public abstract void checkPower();

       public abstract void checkArgument();

       public abstract void checkEntity();

       public abstract void checkPowerAgain();
    }
}
